﻿//Write a program that reads two integer numbers N and K and an array of N elements from the console. Find in the array those K elements that have maximal sum.


using System;

namespace NandKMaxSum
{
    class NandKMaxSum
    {
        static void Main()
        {
            Console.Write("Enter N: ");
            int N = int.Parse(Console.ReadLine()); //get the Lenght
            int[] arrayN = new int[N];  //Create arrayN with [N] lenght
            for (int i = 0; i < N; i++)
            {
                Console.Write("Enter value for Array[{0}]: ", i);
                arrayN[i] = int.Parse(Console.ReadLine());
            }

            Console.Write("Enter K value: ");
            int K = int.Parse(Console.ReadLine());

            while (K > N)
            {
                Console.Write("Enter K value: ");
                K = int.Parse(Console.ReadLine());
            }


            if (K > N)
            {
                Console.WriteLine("Sorry, it seems that K is bigger than N !!!");
                return;
            }

            Array.Sort(arrayN);
            int sum = 0;

            Console.Write("\r\n{ ");

            for (int i = N - K; i < arrayN.Length; i++)
            {
                sum += arrayN[i];
                Console.Write(arrayN[i] + ((i + 1 < arrayN.Length) ? ", " : ""));
            }
            Console.Write(" }");
            Console.Write(" Sum is: {0}\r\n", sum);
        }
    }
}
